<?php
/**
 * Zend Framework
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://framework.zend.com/license/new-bsd
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@zend.com so we can send you a copy immediately.
 *
 * @category   Zend
 * @package    Zend_Auth
 * @subpackage Adapter
 * @copyright  Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 * @version    $Id: Digest.php 23775 2011-03-01 17:25:24Z ralph $
 */


/**
 * @see Zend_Auth_Adapter_Interface
 */
require_once 'Zend/Auth/Adapter/Interface.php';

/**
 * @category   Zend
 * @package    Zend_Auth
 * @subpackage Adapter
 * @copyright  Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 */
class Ext_Auth_Adapter_OAuth implements Zend_Auth_Adapter_Interface
{

	protected $_identity;
	protected $_site;
	
    /**
     * Sets adapter options
     *
     * @param  mixed $username
     * @param  mixed $password
     * @return void
     */
    public function __construct($identity, $site)
    {
    	$this->_identity = $identity;
    	$this->_site = $site;
    }
    

    /**
     * Defined by Zend_Auth_Adapter_Interface
     *
     * @throws Zend_Auth_Adapter_Exception
     * @return Zend_Auth_Result
     */
    public function authenticate()
    {
		$result = array(
            'code'         => Zend_Auth_Result::FAILURE,
            'identity'     => "",
            'messages'     => array(),
     	);

    	$_appModelDbTableUserAccountOAuth = new App_Model_DbTable_User_Account_OAuth();
    	$_userAccountOAuthRow = $_appModelDbTableUserAccountOAuth->getRowByIdentitySite($this->_identity, $this->_site);
    	if(isset($_userAccountOAuthRow['userId']) && $_userAccountOAuthRow['userId'] > 0)
    	{
    		$result['identity'] = $_userAccountOAuthRow['userId'];
    		$result['code'] = Zend_Auth_Result::SUCCESS;
    		
    		$authNamespace = new Zend_Session_Namespace('doudang_oauth');
			unset($authNamespace->_identity);
    		unset($authNamespace->_site);
    	}
    	
    	$authNamespace = new Zend_Session_Namespace('doudang_oauth');
    	$authNamespace->identity = $this->_identity;
    	$authNamespace->site = $this->_site;
    	
    	return new Zend_Auth_Result($result['code'], $result['identity'], $result['messages']);
    }
    
    
}
